import Dialog from '../../components/miniprogram_npm/@vant/weapp/dialog/dialog';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:true,
    userInfo:{},
    userMsg:{},
    show: false, //标记弹出层是否展示
    index:0, //标记voluntary的索引
    voluntaryNum:'',//第几志愿
    //志愿列表
    voluntary:[
      {voluntaryId:'1',title:'第一志愿',schoolName:''},
      {voluntaryId:'2',title:'第二志愿',schoolName:''},
      {voluntaryId:'3',title:'第三志愿',schoolName:''},
      {voluntaryId:'4',title:'第四志愿',schoolName:''},
      {voluntaryId:'5',title:'第五志愿',schoolName:''},
      {voluntaryId:'6',title:'第六志愿',schoolName:''},
      {voluntaryId:'7',title:'第七志愿',schoolName:''},
      {voluntaryId:'8',title:'第八志愿',schoolName:''},
      {voluntaryId:'9',title:'第九志愿',schoolName:''},
      {voluntaryId:'10',title:'第十志愿',schoolName:''},
      {voluntaryId:'11',title:'第十一志愿',schoolName:''},
      {voluntaryId:'12',title:'第十二志愿',schoolName:''},
      {voluntaryId:'13',title:'第十三志愿',schoolName:''},
      {voluntaryId:'14',title:'第十四志愿',schoolName:''},
      {voluntaryId:'15',title:'第十五志愿',schoolName:''},
      {voluntaryId:'16',title:'第十六志愿',schoolName:''},
    ],

    voluntaryItem:{},
    voluntaryList:[],

    //标记是添加还是修改与删除 true为添加 false为修改与删除
    flag:true,
    updateIndex:0,

    activeNames: [],
  },

  //展示遮罩层
  onClickShow(e) {
    const {index} = e.currentTarget.dataset
    this.setData({
      activeNames:[],
      show: true,
      voluntaryItem:this.data.voluntaryList[index],
      updateIndex : index
    });
    if(this.data.voluntaryItem.schoolName==""){
      this.setData({
        flag:true
      })
    }else{
      this.setData({
        flag:false
      })
    }
  },

  //关闭遮罩层
  onClickHide() {
    this.setData({ show: false });
  },

  //展示折叠面板
  show(){
    var num =0;
    var activeNames =[]
    for(var i=0;i<this.data.voluntaryList.length;i++){
      if(this.data.voluntaryList[i]!=null){
        num++
      }
    }
    if(num>2){
      activeNames=['1','2']
    }else if(num==2){
      activeNames=['1','2']
    }
    else if(num==1){
      activeNames=['1']
    }
    this.setData({
      activeNames:activeNames
    })
  },

  //点击展示折叠面板
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },

  // 监听弹出窗口
  showPopup(e) {
    const {index} = e.currentTarget.dataset
    this.setData({
       show: true,
       index:index,
       voluntaryItem:this.data.voluntaryList[index],
       voluntaryNum:this.data.voluntary[index].title
       });
    if(this.data.voluntaryItem.schoolName==""){
      this.setData({
        flag:true
      })
    }else{
      this.setData({
        flag:false
      })
    }
  },

  //关闭弹出窗口
  onClose() {
    this.setData({ show: false });
  },

  //添加志愿
  addUniversity(e){
    const {index} = e.currentTarget.dataset
    var university_order_id = index+1
    var voluntaryList = JSON.stringify(this.data.voluntaryList)
    wx.navigateTo({
      url: '../../addUniversity/pages/addUniversity/addUniversity?university_order_id='+university_order_id+'&voluntaryList='+voluntaryList,
    })
    this.setData({
      show:false
    })
  },

  //修改志愿
  updateUniversity(e){
    var index = this.data.updateIndex
    var voluntaryItem = JSON.stringify(this.data.voluntaryList[index])
    var university_order_id = index+1
    wx.navigateTo({
      url: '/addUniversity/pages/schoolInfo/schoolInfo?voluntaryItem='+voluntaryItem+'&university_order_id='+university_order_id,
    })
    this.setData({
      show:false
    })
  },

  //删除志愿
  deleteUniversity(){
    this.setData({ show: true });
    var index = this.data.updateIndex
    Dialog.confirm({
      message: '确定删除该志愿吗？',
    })
      .then(() => {
        wx.showLoading()
        var university_order_id = index+1
        wx.request({
          url: app.globalData.requestUrl+'/voluntary/deleteVolunteer',
          method: 'GET',
          data: {
            username : this.data.userInfo.nickName,
            university_order_id : university_order_id
          },
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          success: (res) => {
            if(res.data.code == 214){
              var voluntaryList = this.data.voluntaryList
              voluntaryList[index]=null
              this.setData({
                voluntaryList : voluntaryList
              })
              wx:wx.showToast({
                title: '删除志愿成功',
                icon: "none"
              })
            }
            else if(res.data.code == 401){
              wx:wx.showToast({
                title: '服务器异常，稍后再试',
                icon: "none"
              })
            }
          },
          fail: (res) => {
            wx:wx.showToast({
              title: '网络异常，稍后再试',
              icon: "none"
            })
          },
          complete: () => {
            wx.hideLoading()
            this.setData({
              isLoading: false
            })
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  
  //排列志愿
  sort(){
    var length=this.data.voluntaryList.length
    var num=0
    for(var i=0;i<length;i++){
      if(this.data.voluntaryList[i].schoolNumber!=''){
        var schoolName=this.data.voluntaryList[i].schoolName
        var str = 'voluntary['+num+'].schoolName'
        this.setData({
          [str]:schoolName
        })
        num++
      }
    }
    for(var i=num;i<length;i++){
      var str = 'voluntary['+i+'].schoolName'
      this.setData({
        [str]:""
      })
    }
  },

  //获取志愿列表
  getvoluntaryList(){
    wx.request({
      url: app.globalData.requestUrl+'/voluntary/volunteerList',
      method: 'GET',
      data: {
        username: this.data.userInfo.nickName,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        if(res.data.code == 213){
          var obj = res.data.obj.data
          var voluntaryList = []
          for(var i=0;i<obj.length;i++){
            var voluntaryItem = {}
            voluntaryItem.isRansfers=obj[i].isRansfers
            voluntaryItem.schoolName=obj[i].universityName
            if(obj[i].firstMajorCode!=null){
              voluntaryItem.major1Number=obj[i].firstMajorCode
              voluntaryItem.major1Name=obj[i].firstMajorName
            }
            if(obj[i].secondMajorCode!=null){
              voluntaryItem.major2Number=obj[i].secondMajorCode
              voluntaryItem.major2Name=obj[i].secondMajorName
            }
            if(obj[i].thirdMajorCode!=null){
              voluntaryItem.major3Number=obj[i].thirdMajorCode
              voluntaryItem.major3Name=obj[i].thirdMajorName
            }
            if(obj[i].forthMajorCode!=null){
              voluntaryItem.major4Number=obj[i].forthMajorCode
              voluntaryItem.major4Name=obj[i].forthMajorName
            }
            if(obj[i].fifthMajorCode!=null){
              voluntaryItem.major5Number=obj[i].fifthMajorCode
              voluntaryItem.major5Name=obj[i].fifthMajorName
            }
            if(obj[i].sixthMajorCode!=null){
              voluntaryItem.major6Number=obj[i].sixthMajorCode
              voluntaryItem.major6Name=obj[i].sixthMajorName
            }
            var index = obj[i].universityOrderId-1
            voluntaryList[index]=voluntaryItem
          }
          this.setData({
            voluntaryList : voluntaryList
          })
        }
        else if(res.data.code == 401){
          wx:wx.showToast({
            title: '服务器异常，稍后再试',
            icon: "none"
          })
        }
      },
      fail: (res) => {
        wx:wx.showToast({
          title: '网络异常，稍后再试',
          icon: "none"
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if(options.school!=null){
    //   var school = JSON.parse(options.school)
    // }
    //从缓存中获取用户信息
    var that = this;
    wx.getStorage({
      key: "userInfo",
      success:function(res){
        that.setData({
          userInfo: res.data
        });
      },
    })
    wx.getStorage({
      key: "isShow",
      success:function(res){
        var isShow = res.data
        that.setData({
          isShow: isShow
        });
        that.getvoluntaryList()
        //从缓存获取用户高考数据
        wx.getStorage({
          key: "userMsg",
          success:function(res){
            that.setData({
              userMsg: res.data
            });
          },
          fail:function(){
            wx:wx.showToast({
              title: '请填写您的分数信息以分析匹配院校',
              icon: "none"
            })
          }
        });
      },
    })
    setTimeout(()=>{
      this.show()
    },1000)
    // this.show()
    this.sort()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      show:false,
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})